package com.quectel.emp.service.workorder;

import com.quectel.business.common.CommonBusiness;
import com.quectel.business.common.dept.DeptPersonMsgBusiness;
import com.quectel.business.common.workorder.WorkOrderBusiness;
import com.quectel.constant.PushConstants;
import com.quectel.constant.core.dept.DeptPersonMsgConstants;
import com.quectel.constant.core.workorder.WorkOrderConstants;
import com.quectel.core.module.dept.dto.DeptPersonDto;
import com.quectel.core.module.dept.service.DeptPersonService;
import com.quectel.core.module.device.dto.DeviceDto;
import com.quectel.core.module.device.service.DeviceService;
import com.quectel.core.module.workorder.dto.WorkOrderDto;
import com.quectel.core.module.workorder.dto.WorkOrderLogDto;
import com.quectel.core.module.workorder.service.WorkOrderService;
import com.quectel.util.ex.CheckErrorException;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 在这里编写说明
 *
 * @author: cl
 * @email: louis.cao@quectel.com
 * @date: 2022-05-23 11:19:00
 */
@Service
public class ApiWorkOrderService {

    @DubboReference
    private WorkOrderService workOrderService;
    @DubboReference
    private DeviceService deviceService;
    @DubboReference
    private DeptPersonService deptPersonService;

    @Autowired
    private DeptPersonMsgBusiness deptPersonMsgBusiness;
    @Autowired
    private CommonBusiness commonBusiness;
    @Autowired
    private WorkOrderBusiness workOrderBusiness;


    /**
     * 巡检上报工单
     *
     * @param deptPersonId    上报人的id
     * @param categoryAndType 工单分类
     * @param detail          描述
     * @param pictures        图片 json数组
     * @param handlerId       处理工单人员 不为空代表指派
     * @param deviceId        巡更的点位(设备id)
     * @return 数据库最新的工单信息
     */
    public WorkOrderDto patrolReportOrder(
            Long deptPersonId,
            WorkOrderConstants.CategoryAndType categoryAndType,
            String detail,
            String pictures,
            Long handlerId,
            Long deviceId

    ) {
        DeptPersonDto deptPersonDto = deptPersonService.selectById(deptPersonId);
        if (deptPersonDto == null) {
            throw new CheckErrorException("当前员工不存在");
        }


        WorkOrderDto workOrderDto = new WorkOrderDto();

        Date nowDate = new Date();
        //填充基础数据
        workOrderDto.setCreateTime(nowDate);
        workOrderDto.setTenantId(deptPersonDto.getTenantId());
        workOrderDto.setVillageId(deptPersonDto.getVillageId());
        DeviceDto deviceDto = deviceService.selectCacheById(deviceId);
        if (deviceDto != null) {
            workOrderDto.setTenantId(deviceDto.getTenantId());
            workOrderDto.setVillageId(deviceDto.getVillageId());
            workOrderDto.setBuildingId(deviceDto.getBuildingId());
            workOrderDto.setFloorId(deviceDto.getFloorId());
            workOrderDto.setRoomId(deviceDto.getRoomId());
        }


        workOrderDto.setOrderSource(WorkOrderConstants.ORDER_SOURCE_DEPT_PERSON);
        workOrderDto.setSourceId(deptPersonDto.getId());
        workOrderDto.setCategory(categoryAndType.getCategory());
        workOrderDto.setType(categoryAndType.getType());
        workOrderDto.setDetail(detail);
        workOrderDto.setPictures(pictures);

        List<WorkOrderLogDto> logs = new ArrayList<>();
        WorkOrderLogDto initLog = new WorkOrderLogDto();
        // 处理人只有管理员和员工
        initLog.setHandlerType(WorkOrderConstants.ORDER_HANDLER_TYPE_DEPT_PERSON);
        initLog.setHandlerId(deptPersonDto.getId());
        initLog.setOrderStatus(WorkOrderConstants.ORDER_STATUS_INIT);
        initLog.setOrderDes("由员工:" + deptPersonDto.getName() + " 创建工单");
        initLog.setCreateTime(nowDate);
        logs.add(initLog);


        Long workOrderId = null;

        if (handlerId != null) {
            DeptPersonDto handlerDto = deptPersonService.selectById(handlerId);
            String handlerName = null;
            if (handlerDto != null) {
                handlerName = handlerDto.getName();
            }
            WorkOrderLogDto forwordLog = new WorkOrderLogDto();
            forwordLog.setHandlerType(WorkOrderConstants.ORDER_HANDLER_TYPE_DEPT_PERSON);
            forwordLog.setHandlerId(handlerId);
            forwordLog.setOrderStatus(WorkOrderConstants.ORDER_STATUS_FORWARD);
            forwordLog.setOrderDes("由员工:" + deptPersonDto.getName() + " 指派工单给员工:" + handlerName);
            forwordLog.setCreateTime(nowDate);
            logs.add(forwordLog);

            workOrderDto.setHandlerId(handlerId);
            workOrderDto.setStatus(WorkOrderConstants.ORDER_STATUS_FORWARD);
            workOrderDto.setWorkOrderLogDtoList(logs);
            //直接指派第一次处理事件就是创建时间
            workOrderDto.setFirstHandTime(workOrderDto.getCreateTime());

            workOrderId = workOrderService.saveOrUpdateWorkOrderAndLog(workOrderDto);


            deptPersonMsgBusiness.pushMsg(
                    new ArrayList<>(Arrays.asList(deptPersonDto)),
                    DeptPersonMsgConstants.SourceType.WORK_ORDER_MSG,
                    workOrderId,
                    null,
                    commonBusiness.getAddressByVillageInfo(workOrderDto) + "|" + workOrderBusiness.getCategoryAndType(workOrderDto)[0] + ":" + workOrderBusiness.getCategoryAndType(workOrderDto)[1] + "," + workOrderDto.getDetail(),
                    true,
                    new PushConstants.PushType[]{PushConstants.PushType.WECHAT_OFFICIAL}
            );

        } else {
            workOrderDto.setStatus(WorkOrderConstants.ORDER_STATUS_INIT);
            workOrderDto.setHandlerId(null);
            workOrderDto.setWorkOrderLogDtoList(logs);
            workOrderId = workOrderService.saveOrUpdateWorkOrderAndLog(workOrderDto);
        }
        return workOrderService.selectById(workOrderId);
    }
}
